package de.unibi.mmi.homeauto.team1.devices;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import de.unibi.mmi.homeauto.team1.R;
import de.unibi.mmi.homeauto.team1.client.Client;
import de.unibi.mmi.homeauto.team1.constants.BATH;
import de.unibi.mmi.homeauto.team1.constants.BEDROOM;
import de.unibi.mmi.homeauto.team1.constants.CORRIDOR;
import de.unibi.mmi.homeauto.team1.constants.DeviceValues;
import de.unibi.mmi.homeauto.team1.constants.KITCHEN;
import de.unibi.mmi.homeauto.team1.constants.LIVINGROOM;
import de.unibi.mmi.homeauto.team1.drafts.DraftFragment;
import de.unibi.mmi.homeauto.team1.mainview.MainViewActivity;
import de.unibi.mmi.homeauto.team1.rooms.RoomViewPager;

public class FireFragment extends DraftFragment {

	Button all, corridor, livingroom, bathroom, kitchen, bedroom;

	@Override
	protected int getLayoutID() {
		return R.layout.firefragment;
	}

	@Override
	public void loadComponents(View view) {

		((ImageView) view.findViewById(R.id.fire_bathroom_image))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						mva.changeScreen(MainViewActivity.ROOMS_POS,
								RoomViewPager.BATH_POS);
					}
				});

		((ImageView) view.findViewById(R.id.fire_livingroom_image))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						mva.changeScreen(MainViewActivity.ROOMS_POS,
								RoomViewPager.LIVING_ROOM_POS);
					}
				});

		((ImageView) view.findViewById(R.id.fire_bedroom_image))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						mva.changeScreen(MainViewActivity.ROOMS_POS,
								RoomViewPager.BEDROOM_POS);
					}
				});

		((ImageView) view.findViewById(R.id.fire_kitchen_image))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						mva.changeScreen(MainViewActivity.ROOMS_POS,
								RoomViewPager.KITCHEN_POS);
					}
				});

		((ImageView) view.findViewById(R.id.fire_corridor_image))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						mva.changeScreen(MainViewActivity.ROOMS_POS,
								RoomViewPager.CORRIDOR_POS);
					}
				});

		all = (Button) view.findViewById(R.id.smoke_all_button);

		bathroom = (Button) view.findViewById(R.id.smoke_bathroom_button);
		livingroom = (Button) view.findViewById(R.id.smoke_livingroom_button);
		bedroom = (Button) view.findViewById(R.id.smoke_bedroom_button);
		corridor = (Button) view.findViewById(R.id.smoke_corridor_button);
		kitchen = (Button) view.findViewById(R.id.smoke_kitchen_button);

	}

	@Override
	public void serverValuesUpdated() {
		if (Client.getValue(CORRIDOR.SMOKE_ID) == DeviceValues.SMOKE_ALARM) {
			corridor.setText("Alarm");
			corridor.setBackgroundColor(0xffff0000);
		} else if (Client.getValue(CORRIDOR.SMOKE_ID) == DeviceValues.SMOKE_NORMAL) {
			corridor.setText("Bereit");
			corridor.setBackgroundColor(0xff00ff00);
		} else {
			corridor.setText("Defekt");
			corridor.setBackgroundColor(0xffffff00);
		}

		if (Client.getValue(LIVINGROOM.SMOKE_ID) == DeviceValues.SMOKE_ALARM) {
			livingroom.setText("Alarm");
			livingroom.setBackgroundColor(0xffff0000);
		} else if (Client.getValue(LIVINGROOM.SMOKE_ID) == DeviceValues.SMOKE_NORMAL) {
			livingroom.setText("Bereit");
			livingroom.setBackgroundColor(0xff00ff00);
		} else {
			livingroom.setText("Defekt");
			livingroom.setBackgroundColor(0xffffff00);
		}

		if (Client.getValue(BATH.SMOKE_ID) == DeviceValues.SMOKE_ALARM) {
			bathroom.setText("Alarm");
			bathroom.setBackgroundColor(0xffff0000);
		} else if (Client.getValue(BATH.SMOKE_ID) == DeviceValues.SMOKE_NORMAL) {
			bathroom.setText("Bereit");
			bathroom.setBackgroundColor(0xff00ff00);
		} else {
			bathroom.setText("Defekt");
			bathroom.setBackgroundColor(0xffffff00);
		}

		if (Client.getValue(KITCHEN.SMOKE_ID) == DeviceValues.SMOKE_ALARM) {
			kitchen.setText("Alarm");
			kitchen.setBackgroundColor(0xffff0000);
		} else if (Client.getValue(KITCHEN.SMOKE_ID) == DeviceValues.SMOKE_NORMAL) {
			kitchen.setText("Bereit");
			kitchen.setBackgroundColor(0xff00ff00);
		} else {
			kitchen.setText("Defekt");
			kitchen.setBackgroundColor(0xffffff00);
		}

		if (Client.getValue(BEDROOM.SMOKE_ID) == DeviceValues.SMOKE_ALARM) {
			bedroom.setText("Alarm");
			bedroom.setBackgroundColor(0xffff0000);
		} else if (Client.getValue(BEDROOM.SMOKE_ID) == DeviceValues.SMOKE_NORMAL) {
			bedroom.setText("Bereit");
			bedroom.setBackgroundColor(0xff00ff00);
		} else {
			bedroom.setText("Defekt");
			bedroom.setBackgroundColor(0xffffff00);
		}

		if (Client.getValue(BEDROOM.SMOKE_ID) == DeviceValues.SMOKE_ALARM
				|| Client.getValue(BATH.SMOKE_ID) == DeviceValues.SMOKE_ALARM
				|| Client.getValue(LIVINGROOM.SMOKE_ID) == DeviceValues.SMOKE_ALARM
				|| Client.getValue(KITCHEN.SMOKE_ID) == DeviceValues.SMOKE_ALARM
				|| Client.getValue(CORRIDOR.SMOKE_ID) == DeviceValues.SMOKE_ALARM) {
			all.setText("Alarm");
			all.setBackgroundColor(0xffff0000);
		} else if (Client.getValue(BEDROOM.SMOKE_ID) == DeviceValues.SMOKE_ERROR
				|| Client.getValue(BATH.SMOKE_ID) == DeviceValues.SMOKE_ERROR
				|| Client.getValue(LIVINGROOM.SMOKE_ID) == DeviceValues.SMOKE_ERROR
				|| Client.getValue(KITCHEN.SMOKE_ID) == DeviceValues.SMOKE_ERROR
				|| Client.getValue(CORRIDOR.SMOKE_ID) == DeviceValues.SMOKE_ERROR) {
			all.setText("Defekt");
			all.setBackgroundColor(0xffffff00);
		} else {
			all.setText("Bereit");
			all.setBackgroundColor(0xff00ff00);
		}
	}

	@Override
	public String getTitle() {
		return "Rauchmelder";
	}
}